import json

from api.loginApi import LoginApi
import unittest
import requests
import app
from parameterized import parameterized
def build_data():
    file_path=app.base_dir+"/data/login.json"
    list_data=[]
    with open(file=file_path,encoding="utf-8") as f:
        data_json=json.load(f)
        for data in data_json:
            username=data["username"]
            password=data["password"]
            verify_code=data["verify_code"]
            status_code=data["status_code"]
            status=data["status"]
            msg=data["msg"]
            list_data.append((username,password,verify_code,status_code,status,msg))
    return list_data


class TestLogin(unittest.TestCase):
    def setUp(self):
        # 实例化对象
        self.login_api = LoginApi()
        # 创建session对象
        self.session = requests.Session()

    def tearDown(self):
        if self.session:
            self.session.close()
    @parameterized.expand(build_data())
    def test01_login(self,username,password,verify_code,status_code,status,msg):
        response = self.login_api.get_verify(self.session)
        self.assertIn("image", response.headers["Content-Type"])
        self.assertEqual(200, response.status_code)
        login_response = self.login_api.login(self.session, username, password, verify_code)
        self.assertEqual(status_code, login_response.status_code)
        self.assertEqual(status, login_response.json()['status'])
        self.assertEqual(msg, login_response.json()['msg'])

